﻿using System;
using System.Web.Services;
using System.Web.Services.Protocols;
using App.Info;

namespace ServiceData
{
    /// <summary>
    /// Summary description for WebService_BoiHangNgay
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class WebService_BoiHangNgay : System.Web.Services.WebService
    {
        public AuthenticationHeader AuthHeader;

        private bool CheckAuth()
        {
            string user = App.Utility.Config.UserServices;
            string pass = App.Utility.Config.PassServices;
            if (AuthHeader == null) return false;
            if (AuthHeader.Username == user && AuthHeader.Password == pass)
                return true;
            return false;
        }

        [WebMethod(Description = "Update thông tin user"), SoapHeader("AuthHeader")]
        public string Update(string AppID, string UserId, string birthday, string Username, string sToken, string sContent, bool active)
        {
            if (!CheckAuth())
                return "Sai user or password";
            string[] arr = birthday.Split('-');//yyyy-MM-dd
            DateTime dt = new DateTime(Convert.ToInt32(arr[0]), Convert.ToInt32(arr[1]), Convert.ToInt32(arr[2]));
            UserFbInfo info = new UserFbInfo
                                  {
                                      AppID = AppID,
                                      UserId = UserId,
                                      birthday = dt,
                                      Username = Username,
                                      sToken = sToken,
                                      sContent = sContent,
                                      Active = active
                                  };
            if (ServicesFactory.GetInstanceUserFb().Update(info))
            {
                return "1";
            }
            else
            {
                return "0";
            }
        }

        [WebMethod(Description = "Lấy thông tin user"), SoapHeader("AuthHeader")]
        public string SelectAll(string AppId)
        {
            if (!CheckAuth())
                return "Sai user or password";
            string sContent = "";
            var oList = ServicesFactory.GetInstanceUserFb().SelectAll(AppId);
            if (oList != null && oList.Count > 0)
            {
                sContent = Newtonsoft.Json.JsonConvert.SerializeObject(oList);
            }
            return sContent;
        }

        [WebMethod(Description = "Update status send thông qua ID"), SoapHeader("AuthHeader")]
        public string UpdateSttSend1(int id, bool isSendOk)
        {
            if (!CheckAuth())
                return "Sai user or password";
            if (ServicesFactory.GetInstanceUserFb().UpdateSttSend(id, isSendOk))
            {
                return "1";
            }
            else
            {
                return "0";
            }
        }

        [WebMethod(Description = "Update status send thông qua AppId và UserID"), SoapHeader("AuthHeader")]
        public string UpdateSttSend2(string AppId, string UserId, bool isSendOk)
        {
            if (!CheckAuth())
                return "Sai user or password";
            if (ServicesFactory.GetInstanceUserFb().UpdateSttSend(AppId, UserId, isSendOk))
            {
                return "1";
            }
            else
            {
                return "0";
            }
        }
    }
}